﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Configuration;
using System.Web.UI.WebControls;

using MySql.Data.MySqlClient;
using MySql;

namespace doris.Ticketuser {
    public partial class userLogin : System.Web.UI.Page {
        String username;
        String password;
        MySqlConnection con;
        MySqlCommand cmd;

        protected void Page_Load(object sender, EventArgs e) {


        }
        //Check if Username exist
        //If username exist: check if Username and password match
        //Message if Username don't exist or Username and Password don't match  
        //If Username and Password match: Over to main page for users. 
        protected void SubmitButton_Click(object sender, EventArgs e) {
            String u = "";
            String p = "";

            SQLConnection();
            u = UserBox.Text;
            p = PasswordBox.Text;
            MySqlDataReader reader;

            cmd = new MySqlCommand("SELECT Username, Password FROM user", con);

            try {
                con.Open();
                reader = cmd.ExecuteReader();

                while (reader.Read()) {
                    if (u == reader["Username"].ToString()) {
                        setUser(u);
                    }

                    if (p == reader["Password"].ToString()) {
                        setPassword(p);
                    }
                }

                if (getUser() != u) {
                    Label3.Text = "This User doesn't exist";
                } else if (getUser() != u || getPassword() != p) {
                    Label3.Text = "Wrong user or password. Pleas try again";
                } else {
                    Label3.Text = "Worked!";
                }
            } catch (Exception ex) {
                string x = ex.ToString();
            }
        }


        public void setUser(String username) {
            this.username = username;
        }


        public void setPassword(String password) {
            this.password = password;
        }


        public String getUser() {
            return username;
        }


        public String getPassword() {
            return password;
        }


        public void SQLConnection() {
            con = new MySqlConnection(WebConfigurationManager.ConnectionStrings["localConnectionString"].ConnectionString);
        }


    }
}